import 'package:flutter/material.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({Key? key}) : super(key: key);

  @override
  State<ProfileScreen> createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
  // سىمۇلاتسىيە قىلىنغان ئىشلەتكۈچى ئۇچۇرلىرى
  final Map<String, dynamic> _userInfo = {
    'ئىسمى': 'ئابدۇللا ئەخمەت',
    'تېلېفون': '134 567 8901',
    'ئېلخەت': 'example@mail.com',
    'ئادرېس': 'ئۈرۈمچى شەھىرى، تىنچلىق يولى 123-قەۋەت',
    'خىزمەت': 'تىجارەتچى',
    'شىركەت': 'تىجارەت ياردەمچىسى شىركىتى',
  };

  // تەھرىرلەش ھالىتى
  bool _isEditing = false;
  final Map<String, TextEditingController> _controllers = {};

  @override
  void initState() {
    super.initState();
    // كونتروللېرلارنى تەييارلاش
    for (var key in _userInfo.keys) {
      _controllers[key] =
          TextEditingController(text: _userInfo[key].toString());
    }
  }

  @override
  void dispose() {
    // كونتروللېرلارنى تازىلاش
    for (var controller in _controllers.values) {
      controller.dispose();
    }
    super.dispose();
  }

  // ئۇچۇرلارنى ساقلاش
  void _saveChanges() {
    setState(() {
      for (var key in _userInfo.keys) {
        _userInfo[key] = _controllers[key]!.text;
      }
      _isEditing = false;
    });
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('ئۇچۇرلار ساقلاندى')),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('شەخسىي ئۇچۇر'),
        actions: [
          IconButton(
            icon: Icon(_isEditing ? Icons.save : Icons.edit),
            onPressed: () {
              if (_isEditing) {
                _saveChanges();
              } else {
                setState(() {
                  _isEditing = true;
                });
              }
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              // پروفايل رەسىمى ۋە ئىسىم
              Center(
                child: Column(
                  children: [
                    Container(
                      width: 120,
                      height: 120,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: Colors.blue.shade50,
                        border: Border.all(
                          color: Colors.blue.shade200,
                          width: 2.0,
                        ),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.blue.withOpacity(0.1),
                            blurRadius: 10,
                            spreadRadius: 2,
                          ),
                        ],
                      ),
                      child: const Icon(
                        Icons.person,
                        size: 70,
                        color: Colors.blue,
                      ),
                    ),
                    const SizedBox(height: 16),
                    !_isEditing
                        ? Text(
                            _userInfo['ئىسمى'] ?? '',
                            style: const TextStyle(
                              fontSize: 24,
                              fontWeight: FontWeight.bold,
                            ),
                          )
                        : Container(
                            constraints: const BoxConstraints(maxWidth: 250),
                            child: TextFormField(
                              controller: _controllers['ئىسمى'],
                              textAlign: TextAlign.center,
                              textDirection: TextDirection.rtl,
                              decoration: InputDecoration(
                                hintText: 'ئىسمىڭىزنى كىرگۈزۈڭ',
                                hintTextDirection: TextDirection.rtl,
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10),
                                ),
                              ),
                            ),
                          ),
                  ],
                ),
              ),

              const SizedBox(height: 40),

              // ئىشلەتكۈچى ئۇچۇرلىرى كارتا
              Card(
                elevation: 2,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        'ئاساسىي ئۇچۇرلار',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                        textAlign: TextAlign.right,
                      ),
                      const Divider(height: 30),
                      ..._buildInfoFields(),
                    ],
                  ),
                ),
              ),

              const SizedBox(height: 24),

              // بىخەتەرلىك تەڭشەكلىرى كارتا
              Card(
                elevation: 2,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        'بىخەتەرلىك تەڭشەكلىرى',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                        textAlign: TextAlign.right,
                      ),
                      const Divider(height: 30),
                      _buildSecurityOption(
                        'پارول ئۆزگەرتىش',
                        Icons.lock_outline,
                        () {
                          // پارول ئۆزگەرتىش دىئالوگىنى كۆرسىتىش
                          _showChangePasswordDialog(context);
                        },
                      ),
                      _buildSecurityOption(
                        'ئىككى باسقۇچلۇق دەلىللەش',
                        Icons.security,
                        () {
                          // ئىككى باسقۇچلۇق دەلىللەش تەڭشىكى
                          _showTwoFactorDialog(context);
                        },
                      ),
                      _buildSecurityOption(
                        'كىرىش تارىخى',
                        Icons.history,
                        () {
                          // كىرىش تارىخى بېتىگە يۆتكەش
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                                content: Text('بۇ ئىقتىدار تېخى ئىشلەنمىدى')),
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ),

              const SizedBox(height: 32),

              if (!_isEditing)
                ElevatedButton.icon(
                  onPressed: () {
                    setState(() {
                      _isEditing = true;
                    });
                  },
                  icon: const Icon(Icons.edit),
                  label: const Text('ئۇچۇرلارنى تەھرىرلەش'),
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }

  // ئۇچۇر بۆلەكلىرىنى قۇرۇش
  List<Widget> _buildInfoFields() {
    List<Widget> fields = [];

    _userInfo.forEach((key, value) {
      if (key != 'ئىسمى') {
        // ئىسىم يۇقىرىدا كۆرسىتىلدى
        fields.add(
          Padding(
            padding: const EdgeInsets.only(bottom: 16),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              textDirection: TextDirection.rtl,
              children: [
                SizedBox(
                  width: 100,
                  child: Text(
                    '$key:',
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.black87,
                    ),
                    textAlign: TextAlign.right,
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: !_isEditing
                      ? Text(
                          value.toString(),
                          textAlign: TextAlign.right,
                          style: const TextStyle(color: Colors.black87),
                        )
                      : TextFormField(
                          controller: _controllers[key],
                          textDirection: TextDirection.rtl,
                          textAlign: TextAlign.right,
                          decoration: InputDecoration(
                            hintText: key,
                            hintTextDirection: TextDirection.rtl,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                            contentPadding: const EdgeInsets.symmetric(
                              horizontal: 12,
                              vertical: 8,
                            ),
                          ),
                        ),
                ),
              ],
            ),
          ),
        );
      }
    });

    return fields;
  }

  // بىخەتەرلىك تاللانمىلىرى
  Widget _buildSecurityOption(String title, IconData icon, VoidCallback onTap) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 12),
        child: Row(
          textDirection: TextDirection.rtl,
          children: [
            Icon(icon, color: Colors.blue.shade700),
            const SizedBox(width: 16),
            Text(
              title,
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
              ),
            ),
            const Spacer(),
            const Icon(Icons.arrow_back_ios, size: 16, color: Colors.grey),
          ],
        ),
      ),
    );
  }

  // پارول ئۆزگەرتىش دىئالوگى
  void _showChangePasswordDialog(BuildContext context) {
    final currentPasswordController = TextEditingController();
    final newPasswordController = TextEditingController();
    final confirmPasswordController = TextEditingController();

    showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('پارول ئۆزگەرتىش', textAlign: TextAlign.center),
          content: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TextField(
                  controller: currentPasswordController,
                  obscureText: true,
                  textDirection: TextDirection.rtl,
                  decoration: InputDecoration(
                    labelText: 'نۆۋەتتىكى پارول',
                    labelStyle: const TextStyle(
                      fontSize: 14,
                    ),
                    alignLabelWithHint: true,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: newPasswordController,
                  obscureText: true,
                  textDirection: TextDirection.rtl,
                  decoration: InputDecoration(
                    labelText: 'يېڭى پارول',
                    labelStyle: const TextStyle(
                      fontSize: 14,
                    ),
                    alignLabelWithHint: true,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: confirmPasswordController,
                  obscureText: true,
                  textDirection: TextDirection.rtl,
                  decoration: InputDecoration(
                    labelText: 'يېڭى پارولنى قايتا كىرگۈزۈڭ',
                    labelStyle: const TextStyle(
                      fontSize: 14,
                    ),
                    alignLabelWithHint: true,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                    ),
                  ),
                ),
              ],
            ),
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('ئىمتىھان قىلىش'),
            ),
            ElevatedButton(
              onPressed: () {
                // پارولنى تەكشۈرۈش ۋە ئۆزگەرتىش لوگىكىسى
                if (newPasswordController.text ==
                    confirmPasswordController.text) {
                  Navigator.of(context).pop();
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(content: Text('پارول ئۆزگەرتىلدى')),
                  );
                } else {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                        content: Text(
                            'يېڭى پارول بىلەن قايتا كىرگۈزۈلگەن پارول ئوخشاش ئەمەس')),
                  );
                }
              },
              child: const Text('ساقلاش'),
            ),
          ],
        );
      },
    );
  }

  // ئىككى باسقۇچلۇق دەلىللەش دىئالوگى
  void _showTwoFactorDialog(BuildContext context) {
    bool isTwoFactorEnabled = false;

    showDialog(
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return AlertDialog(
              title: const Text('ئىككى باسقۇچلۇق دەلىللەش',
                  textAlign: TextAlign.center),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SwitchListTile(
                    title: const Text('قوزغىتىش', textAlign: TextAlign.right),
                    value: isTwoFactorEnabled,
                    onChanged: (value) {
                      setState(() {
                        isTwoFactorEnabled = value;
                      });
                    },
                  ),
                  const SizedBox(height: 16),
                  Text(
                    isTwoFactorEnabled
                        ? 'ئىككى باسقۇچلۇق دەلىللەش قوزغىتىلدى. كىرگەندە دەلىللەش كودى تەلەپ قىلىنىدۇ.'
                        : 'ئىككى باسقۇچلۇق دەلىللەشنى قوزغىتىش سىزنىڭ ھېساباتىڭىزنىڭ بىخەتەرلىكىنى ئاشۇرىدۇ.',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey[600],
                    ),
                  ),
                ],
              ),
              actions: [
                TextButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: const Text('ۋاز كەچ'),
                ),
                ElevatedButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text(
                          isTwoFactorEnabled
                              ? 'ئىككى باسقۇچلۇق دەلىللەش قوزغىتىلدى'
                              : 'ئىككى باسقۇچلۇق دەلىللەش تاقالدى',
                        ),
                      ),
                    );
                  },
                  child: const Text('ساقلاش'),
                ),
              ],
            );
          },
        );
      },
    );
  }
}
